package com.vq.util;

public class SecretKey {
    /**
     * 加密
     * @param plaintext
     * @return
     */
    static public String encipher(String plaintext)
    {
        StringBuilder ciphertext= new StringBuilder();
        for (int i = 0; i < plaintext.length(); ++i)
        {
            int x = (int) (Math.random()*10);
            char k = (char) x;//密钥
            ciphertext.append(k);
            char y = (char) (plaintext.charAt(i) - x);//原文
            ciphertext.append(y);
        }
        return ciphertext.toString();
    }
    /**
     * 解密
     * @param ciphertext
     * @return
     */
    static public String decipher(String ciphertext)
    {
        StringBuilder plaintext = new StringBuilder();
        for (int i = 0; i <= ciphertext.length()-2; i+=2)
        {
            char y = (char) (ciphertext.charAt(i + 1) + ciphertext.charAt(i));//通过密钥解密
            plaintext.append(y);
        }
        return plaintext.toString();
    }
}
